<?php

if (!defined("JOYEASY"))
    exit("Access Denied");

class IndexAction extends BaseAction {

    function _initialize() {
        parent::_initialize();

        if (!$this->_userid) {
            $this->assign('jumpUrl', U('User/Login/index'));
            $this->error(L('nologin'));
        }
        $this->dao = M('User');
        $this->assign('bcid', 0);
        $user = $this->dao->find($this->_userid);
        $this->assign('vo', $user);
        unset($_POST['status']);
        unset($_POST['groupid']);
        unset($_POST['amount']);
        unset($_POST['point']);
    }

    public function index() {
        $this->display();
    }

    public function profile() {
        if ($_POST['dosubmit']) {
            $_POST['id'] = $this->_userid;
            if (!$this->dao->create($_POST)) {
                $this->error($this->dao->getError());
            }
            $this->dao->update_time = time();
            $this->dao->last_ip = get_client_ip();
            $result = $this->dao->save();
            if (false !== $result) {
                $this->success(L('do_success'));
            } else {
                $this->error(L('do_error'));
            }
            exit;
        }
        $this->display();
    }

    public function avatar() {

        if ($_POST['dosubmit']) {

            $_POST['id'] = $this->_userid;
            if (!$this->dao->create($_POST)) {
                $this->error($this->dao->getError());
            }
            $this->dao->update_time = time();
            $this->dao->last_ip = get_client_ip();
            $result = $this->dao->save();
            if (false !== $result) {
                if ($_POST['aid']) {
                    $Attachment = M('Attachment');
                    $aids = implode(',', $_POST['aid']);
                    $data['userid'] = $this->_userid;
                    $data['catid'] = 0;
                    $data['status'] = '1';
                    $Attachment->where("aid in (" . $aids . ")")->save($data);
                }

                $this->success(L('do_success'));
            } else {
                $this->error(L('do_error'));
            }
        }

        $joyeasy_auth_key = sysmd5(C('ADMIN_ACCESS') . $_SERVER['HTTP_USER_AGENT']);
        $joyeasy_auth = authcode('0-1-0-1-jpeg,jpg,png,gif-3-0', 'ENCODE', $yourphp_auth_key);
        $this->assign('joyeasy_auth', $joyeasy_auth);
        $this->display();
    }

    public function password() {
        if ($_POST['dosubmit']) {
            if (md5($_POST['verify']) != $_SESSION['verify']) {
                $this->error(L('error_verify'));
            }
            if ($_POST['password'] != $_POST['repassword']) {
                $this->error(L('password_repassword'));
            }
            $map = array();
            $map['password'] = sysmd5($_POST['oldpassword']);
            if (isset($this->_userid)) {
                $map['id'] = $this->_userid;
            } elseif (isset($this->_username)) {
                $map['username'] = $this->_username;
            }
            //检查用户
            if (!$this->dao->where($map)->field('id')->find()) {
                $this->error(L('error_oldpassword'));
            } else {
                $this->dao->email = $_POST['email'];
                $this->dao->id = $this->_userid;
                $this->dao->update_time = time();
                $this->dao->password = sysmd5($_POST['password']);
                $r = $this->dao->save();
                $this->assign('jumpUrl', U('User/Index/password'));
                if ($r) {
                    $this->success(L('do_success'));
                } else {
                    $this->error(L('do_error'));
                }
            }
            exit;
        }
        $this->display();
    }

    function add_article() {
        $article = M("Category");
        $cat = $article->where("`moduleid` = 2 and `parentid` != 0")->select();
        $this->assign('cat', $cat);
        $joyeasy_auth_key = sysmd5(C('ADMIN_ACCESS') . $_SERVER['HTTP_USER_AGENT']);
        $joyeasy_auth = authcode('0-1-0-1-jpeg,jpg,png,gif-3-0', 'ENCODE', $yourphp_auth_key);
        $this->assign('joyeasy_auth', $joyeasy_auth);
        $this->display();
    }

//    function all_article() {
//        import('@.ORG.Page');
//        $article = M("Article");
//        $where = "`userid` = " . $_COOKIE['joyeasy_userid'];
//        $count = $article->join('`zx_category` ON `zx_Article`.catid = `zx_category`.id')->where($where)->count();
//        $page = new Page($count, 20);
//        $show = $page->show();
//        $this->assign("page", $show);
//        $list = $article->join('`zx_category` ON `zx_Article`.catid = `zx_category`.id')->order('`zx_Article`.id')->where($where)
//                        ->limit($page->firstRow . ',' . $page->listRows)->field('zx_category.catname as catname,zx_Article.id as id,zx_Article.title as title,zx_Article.hits as hits,zx_Article.username as username')->select();
//        $this->assign('list', $list);
//        $this->display();
//    }
    function all_article() {
        import('@.ORG.Page');
        $article = M("Article");
        $where = "`userid` = " . $_COOKIE['joyeasy_userid'];
        $count = $article->where($where)->count();
        $page = new Page($count, 20);
        $show = $page->show();
        $this->assign("page", $show);
        $list = $article->order('id')->where($where)
                        ->limit($page->firstRow . ',' . $page->listRows)->select();
        $this->assign('list', $list);
        $article = M("Category");
        $cat = $article->where("`moduleid` = 2 and `parentid` != 0")->select();
        $this->assign('cat', $cat);
        $this->display();
    }

    function edit_article() {
        if (!empty($_POST)) {
            $article = M("Article");
            $article->create();
            $r = $article->save();
            if ($r) {
                $this->success(L('do_success'));
            } else {
                $this->error(L('do_error'));
            }
        } else {
            $id = $_GET['id'];
            $article = M("Category");
            $cat = $article->where("`moduleid` = 2 and `parentid` != 0")->select();
            $this->assign('cat', $cat);
            $article = M("Article");
            $vo = $article->where('id = ' . $id)->find();
            $this->assign('vo', $vo);
            $joyeasy_auth_key = sysmd5(C('ADMIN_ACCESS') . $_SERVER['HTTP_USER_AGENT']);
            $joyeasy_auth = authcode('0-1-0-1-jpeg,jpg,png,gif-3-0', 'ENCODE', $yourphp_auth_key);
            $this->assign('joyeasy_auth', $joyeasy_auth);
            $this->display();
        }
    }

    function del_article() {
        $id = $_GET['id'];
        $article = M("Article");
        $del = $article->where('id = ' . $id)->delete();
        if ($del) {
            $this->success(L('do_success'));
        } else {
            $this->error(L('do_error'));
        }
    }

    function del_all() {
        if (!empty($_POST['ids'])) {
            $ids = implode(',', $_POST['ids']);
            $article = M("Article");
            $del = $article->where('id in(' . $ids . ')')->delete();
            if ($del) {
                $this->success(L('do_success'));
            } else {
                $this->error(L('do_error'));
            }
        } else {
            $this->error(L('do_error'));
        }
    }

}

?>